### This File Reproduces Table 1 column 5 (Replication of Adams with well-measured parties)
### JBS 06 September 2018

rm(list=ls())

load("CHES_RWG_1999_2010_alldimensions.Rda")

adams <- read.dta("replication_data_final_AEST.dta")

parlgov <- read.dta("parlgov_partyids_NEW_shortStata12.dta")

parlgov <- parlgov[,c("partyid","cmp","country_name")]

ches.parlgov.1 <- merge(final[[3]],parlgov,by.x=c("countryname","partyid"),by.y=c("country_name","partyid"),all.x=TRUE)
ches.parlgov.2 <- merge(final[[4]],parlgov,by.x=c("countryname","partyid"),by.y=c("country_name","partyid"),all.x=TRUE)


ches.parlgov.1<-ches.parlgov.1[complete.cases(ches.parlgov.1),]

ches.parlgov.1$cmp[ches.parlgov.1$cmp==14221] <- 14223
ches.parlgov.1$cmp[ches.parlgov.1$cmp==22220] <- 22210
ches.parlgov.1$cmp[ches.parlgov.1$cmp== 22525| ches.parlgov.1$cmp== 22523] <- 22521
ches.parlgov.1$cmp[ches.parlgov.1$cmp== 51420] <- 51421
ches.parlgov.1$cmp[ches.parlgov.1$cmp== 22524] <- 22320
ches.parlgov.1$cmp[ches.parlgov.1$cmp== 41112] <- 41113
ches.parlgov.1$cmp[ches.parlgov.1$cmp== 31523| ches.parlgov.1$cmp== 31522] <- 31624
ches.parlgov.1 <- ches.parlgov.1[ches.parlgov.1$partyid!=308,] # drop CSU
ches.parlgov.1 <-ches.parlgov.1[!duplicated(ches.parlgov.1), ]


ches.parlgov.2<-ches.parlgov.2[complete.cases(ches.parlgov.2),]

ches.parlgov.2$cmp[ches.parlgov.2$cmp==14221] <- 14223
ches.parlgov.2$cmp[ches.parlgov.2$cmp==22220] <- 22210
ches.parlgov.2$cmp[ches.parlgov.2$cmp== 22525| ches.parlgov.2$cmp== 22523] <- 22521
ches.parlgov.2$cmp[ches.parlgov.2$cmp== 51420] <- 51421
ches.parlgov.2$cmp[ches.parlgov.2$cmp== 22524] <- 22320
ches.parlgov.2$cmp[ches.parlgov.2$cmp== 41112] <- 41113
ches.parlgov.2$cmp[ches.parlgov.2$cmp== 31523| ches.parlgov.2$cmp== 31522] <- 31624
ches.parlgov.2 <- ches.parlgov.2[ches.parlgov.2$partyid!=308,] # drop CSU
ches.parlgov.2 <-ches.parlgov.2[!duplicated(ches.parlgov.2), ]



dta2006<- ches.parlgov.1
dta2010<- ches.parlgov.2

dta2006$countryname[dta2006$country_id==1] <- "Belgium"
dta2006$countryname[dta2006$country_id==2] <- "Denmark"
dta2006$countryname[dta2006$country_id==3] <- "Germany"
dta2006$countryname[dta2006$country_id==4] <- "Greece"
dta2006$countryname[dta2006$country_id==5] <- "Spain"
dta2006$countryname[dta2006$country_id==6] <- "France"
dta2006$countryname[dta2006$country_id==7] <- "Ireland"
dta2006$countryname[dta2006$country_id==8] <- "Italy"
dta2006$countryname[dta2006$country_id==10] <- "Netherlands"
dta2006$countryname[dta2006$country_id==11] <- "United Kingdom"
dta2006$countryname[dta2006$country_id==12] <- "Portugal"
dta2006$countryname[dta2006$country_id==13] <- "Austria"
dta2006$countryname[dta2006$country_id==14] <- "Finland"
dta2006$countryname[dta2006$country_id==16] <- "Sweden"

dta2010$countryname[dta2010$country==1] <- "Belgium"
dta2010$countryname[dta2010$country==2] <- "Denmark"
dta2010$countryname[dta2010$country==3] <- "Germany"
dta2010$countryname[dta2010$country==4] <- "Greece"
dta2010$countryname[dta2010$country==5] <- "Spain"
dta2010$countryname[dta2010$country==6] <- "France"
dta2010$countryname[dta2010$country==7] <- "Ireland"
dta2010$countryname[dta2010$country==8] <- "Italy"
dta2010$countryname[dta2010$country==10] <- "Netherlands"
dta2010$countryname[dta2010$country==11] <- "United Kingdom"
dta2010$countryname[dta2010$country==12] <- "Portugal"
dta2010$countryname[dta2010$country==13] <- "Austria"
dta2010$countryname[dta2010$country==14] <- "Finland"
dta2010$countryname[dta2010$country==16] <- "Sweden"


eupos.rwg.2006 <- dta2006[,c("cmp","eu_position.rwg")]
eupos.rwg.2006$year <- 2004

eupos.rwg.2010 <- dta2010[,c("cmp","eu_position.rwg")]
eupos.rwg.2010$year <- 2009

eupos.rwg.m <-rbind(eupos.rwg.2006, eupos.rwg.2010)

names(adams)[7]<-"cmp"
adams.rwg <- merge(adams,eupos.rwg.m, by = c("cmp","year"),all.x=T)

adams.rwg[order(adams.rwg$eu_position.rwg),c("country_name","year","partyname","eu_position.rwg")]

adams.rwg <- adams.rwg[is.na(adams.rwg$toofew),]

length(adams.rwg[adams.rwg$eu_position.rwg<0.7,1])

badrwg <- adams.rwg[adams.rwg$eu_position.rwg<0.7,]
badrwg <- badrwg[order(badrwg$eu_position.rwg),]

#pdf(file="AdamsPartiesRwg.pdf",width=9,height=6)
#par(mfrow=c(1,2))
#hist(adams.rwg$eu_position.rwg, main="Histogram of EU Position Rwg \n for Parties Used by Adams et al.", xlab="EU Position Rwg Across All Parties",col="gray")
#par(mai=c(1,1.5,1,1))
#barplot(badrwg$eu_position.rwg, main="Parties with Rwg Less Than 0.7\n Used By Adams et al.", horiz=T, names.arg= paste(badrwg$country_name,badrwg$partyname),las=1,cex.names=0.8,xlab="Rwg")
#dev.off()

data <- adams.rwg[adams.rwg$eu_position.rwg>0.7,]

adams.table2.col3 <- lm(party_ch_all_voters_t~ emp_ch1_10+ interpolated_expert_ch1_10,data=adams.rwg) # cluster(cmpcode)
summary(adams.table2.col3)

adams.table2.col3.rep <- lm( party_ch_all_voters_t~ emp_ch1_10+ interpolated_expert_ch1_10,data=data) # cluster(cmpcode)
summary(adams.table2.col3.rep)
